package com.wyc.apidriver.service.impl;

import com.wyc.apidriver.feign.DriverUserFeignClient;
import com.wyc.apidriver.feign.MapFeignClient;
import com.wyc.apidriver.service.PointUploadService;
import com.wyc.internalCommon.request.CarDTO;
import com.wyc.internalCommon.request.CarPointUploadDTO;
import com.wyc.internalCommon.request.PointDTO;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.internalCommon.util.PageUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PointUploadServiceImpl implements PointUploadService {

    @Autowired
    private DriverUserFeignClient driverUserFeignClient;

    @Autowired
    private MapFeignClient mapFeignClient;

    /**
     * 车辆上传轨迹
     * @param carPointUploadDTO
     * @return
     */
    @Override
    public ApiResult upload(CarPointUploadDTO carPointUploadDTO) {
        // 车辆id
        Long carId = carPointUploadDTO.getCarId();
        // 根据车辆id查询车辆信息，获取tid,trid
        HashMap<String, Object> map = new HashMap<>();
        map.put("carId",carId);
        PageUtils pageUtils = driverUserFeignClient.list(map);
        JSONObject pageUtilsJson = JSONObject.fromObject(pageUtils);
        JSONArray list = pageUtilsJson.getJSONArray("list");
        JSONObject jsonObject = list.getJSONObject(0);

        String tid = jsonObject.getString("tid");
        String trid = jsonObject.getString("trid");

        // 上传轨迹id
        PointDTO pointDTO = new PointDTO();
        pointDTO.setTid(tid);
        pointDTO.setTrid(trid);
        pointDTO.setPoints(carPointUploadDTO.getPoints());
        mapFeignClient.upload(pointDTO);

        return ApiResult.success();
    }
}
